package com.just521.xinshoutong.view;

import java.io.Serializable;
import java.util.Calendar;

import com.just521.xinshoutong.calendar.ChineseCalendar;
import com.just521.xinshoutong.common.CommonDefine;
import com.just521.xinshoutong.json.Notify;




public class CRemindInfo implements Serializable
{
	public static class CRemindType{
		public static int 						Remind_Type_Invalid		=	-1;
		public static int 						Remind_Type_CountDown	=	1;
		public static int 						Remind_Type_Week		=	2;
		public static int 						Remind_Type_Once		=	3;
		public static int 						Remind_Type_Month		=	4;
		public static int 						Remind_Type_Year		=	5;
		
		
		public static int 						WeekInvalid				=	-1;
		public static int 						WeekNext				=	1;
		public static int 						WeekCurrent				=	2;
	}
	public  String 	Interval	=	"间隔";
	public  String 	Cycel		=	"循环";
	public  String 	Once		=	"单次";
	
	public static final long ONE_WEEK_TIME		=	60 * 60 * 24 * 7 * 1000;
	
	private static final long serialVersionUID	=	-7060210544600464481L;
	
	Byte		m_Week[]	=	{0,0, 0, 0, 0, 0, 0 };	//从周日到周六
	
	int			m_iType;				//间隔:1,循环:2,单次:3,无效:-1
	public long		m_lTime = 0;				//如果单次提醒表示日期年月日
										//如果是循环提醒则高4位表示小时，低4位表示分钟
	int			m_iRemindAble;
	int			m_iIsRemind;
	
	Byte		m_MonthDay = 0;
	
	
	public		boolean m_bNongLi = false;
	
	
	public	void	setNormalTime( int iHour, int iMinute, int iYear, int iMonth, int iDay, boolean bNongLi )
	{
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());

		clCalendar.set(Calendar.SECOND, 0 );
		clCalendar.set(Calendar.MILLISECOND, 0 );	
		
		clCalendar.set(Calendar.YEAR, iYear );
		clCalendar.set(Calendar.MONTH, iMonth );
		clCalendar.set(Calendar.DAY_OF_MONTH, iDay );
		clCalendar.set(Calendar.HOUR_OF_DAY, iHour );
		clCalendar.set(Calendar.MINUTE, iMinute );
		
		m_lTime	=	clCalendar.getTimeInMillis();
		
		 m_bNongLi = bNongLi;
		 
		 if(bNongLi)
			 m_Week[6] =1;
		 else 
			 m_Week[6] =0;
	}		
	
	public	void	setCutDownTime( int iHour, int iMinute )
	{
//		Calendar clCalendar	=	Calendar.getInstance();
//		clCalendar.setTimeInMillis(System.currentTimeMillis());
//	
//		m_lTime	=	clCalendar.getTimeInMillis();
//		
//		m_lTime	+=	( ( iHour * 60 + iMinute ) * 60 ) * 1000;
		
		m_Week[0] = (byte)iHour;
		m_Week[1] = (byte)iMinute;
		
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());	
		long	lTime	=	clCalendar.getTimeInMillis();
		
		lTime	+=	( ( iHour * 60 + iMinute ) * 60 ) * 1000;
		
		iHour	<<=	16;
		iHour	+=	iMinute;
		
		m_lTime	=	lTime;
		
	}
	
	public	void	setWeekTime( int iHour, int iMinute, Byte week[] )
	{
		iHour	<<=	16;
		iHour	+=	iMinute;
		
		m_lTime	=	iHour;
		
		int iLength	=	m_Week.length;
		for( int i = 0; i < iLength; ++i )
		{
			m_Week[ i ]	=	week[ i ];
		}
	}

	public	void	setWeekTime( Notify notify )
	{
		m_lTime	=	notify.getNotifytime();
		
		int iLength	=	m_Week.length;
		for( int i = 0; i < iLength; ++i )
		{
			m_Week[ i ]	=	notify.getDays().get(i).equals("1")?(byte)1:0;
		}
		
		
	}
	public	void	setYearTime( Notify notify )
	{
		m_lTime	=	notify.getNotifytime();
		
		int iLength	=	m_Week.length;
		for( int i = 0; i < iLength; ++i )
		{
			m_Week[ i ]	=	notify.getDays().get(i).equals("1")?(byte)1:0;
		}
		
		if( notify.getDays().get(6).equals("1") )
			m_bNongLi = true;
		else 
			m_bNongLi = false;	
	}
	
	public	void	setMonthTime( Notify notify )
	{
		m_lTime	=	notify.getNotifytime();
		
		int iLength	=	m_Week.length;
		for( int i = 0; i < iLength; ++i )
		{
			m_Week[ i ]	=	notify.getDays().get(i).equals("1")?(byte)1:0;
		}
		
		m_MonthDay = m_Week[0];
			
	}	
	
	public	void setMonthTime( int iHour, int iMinute, Byte iDay )
	{
		iHour	<<=	16;
		iHour	+=	iMinute;
		
		m_lTime	=	iHour;
		m_MonthDay = iDay;
		
		m_Week[0] = m_MonthDay;
	}
	
	public	void setYearTime( int iHour, int iMinute, int iYear, Byte iMonth, Byte iDay, boolean bNongLi )
	{
		iHour	<<=	16;
		iHour	+=	iMinute;
		
		m_lTime	=	iHour;
		m_MonthDay = iDay;
		
		m_Week[0] = m_MonthDay;
		m_Week[1] = iMonth;
		
		m_bNongLi = bNongLi;
		
		 if(bNongLi)
			 m_Week[6] =1;
		 else 
			 m_Week[6] =0;
	}
	
	public	void	getNormalTime( CDateAndTime clCDateAndTime )
	{
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis( m_lTime );
		CDateAndTime tempDT = new CDateAndTime(); 
		int year 	= clCalendar.get(Calendar.YEAR);
		int month	= clCalendar.get(Calendar.MONTH);
		int day 	= clCalendar.get(Calendar.DAY_OF_MONTH);
		
		clCDateAndTime.iYear	=	year;
		clCDateAndTime.iMonth	=	month;
		clCDateAndTime.iDay		=	day;
		clCDateAndTime.iHour	=	clCalendar.get(Calendar.HOUR_OF_DAY);
		clCDateAndTime.iMinute	=	clCalendar.get(Calendar.MINUTE);
		clCDateAndTime.bNongLi  =   m_bNongLi;
		
	}
	public	void	getNormalTime( long lTime ,CDateAndTime clCDateAndTime )
	{
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis( lTime );

		CDateAndTime tempDT = new CDateAndTime(); 
		int year 	= clCalendar.get(Calendar.YEAR);
		int month	= clCalendar.get(Calendar.MONTH);
		int day 	= clCalendar.get(Calendar.DAY_OF_MONTH);
		if( m_bNongLi 
			|| (( m_iType == CRemindType.Remind_Type_Once )&&(m_Week[6]==1 ))
			|| (( m_iType == CRemindType.Remind_Type_Year )&&(m_Week[6]==1 )))		
		{
			ChineseCalendar.sCalendarLundarToSolar(year, month+1, day,  tempDT);
			year = tempDT.iYear;
			month= tempDT.iMonth-1;
			day = tempDT.iDay;	
		}
		clCDateAndTime.iYear	=	year;
		clCDateAndTime.iMonth	=	month;
		clCDateAndTime.iDay		=	day;
		clCDateAndTime.iHour	=	clCalendar.get(Calendar.HOUR_OF_DAY);
		clCDateAndTime.iMinute	=	clCalendar.get(Calendar.MINUTE);
		clCDateAndTime.bNongLi  =   m_bNongLi;
	}	

	
	public	long	getCutDownTime( )
	{
//		Calendar clCalendar	=	Calendar.getInstance();
//		clCalendar.setTimeInMillis(System.currentTimeMillis());
//		
//		long	lCur	=	clCalendar.getTimeInMillis();
//		long	lTemp	=	m_lTime	-	lCur;
//		
//		clCDateAndTime.iHour	=	(int)(lTemp / 1000 / 60/ 60 );
//		lTemp   -=	( clCDateAndTime.iHour * 1000 * 60 * 60 );
//		
//		clCDateAndTime.iMinute	=	(int)( lTemp / 1000 / 60 ) + 1;
		
//		clCDateAndTime.iHour	=	(int) (m_lTime >> 16);
//		clCDateAndTime.iMinute	=	(int) m_lTime & 0x0000ffff;
	
		return m_lTime;
		
	}	
	
	public	void	getMonthTime( CDateAndTime clCDateAndTime)
	{
		clCDateAndTime.iHour	=	(int) (m_lTime >> 16);
		clCDateAndTime.iMinute	=	(int) m_lTime & 0x0000ffff;
		clCDateAndTime.iDay     =    m_MonthDay;
		
	}
	
	public	void	getYearTime( CDateAndTime clCDateAndTime)
	{
		clCDateAndTime.iHour	=	(int) (m_lTime >> 16);
		clCDateAndTime.iMinute	=	(int) m_lTime & 0x0000ffff;
		clCDateAndTime.iDay     =    m_Week[0];
		clCDateAndTime.iMonth   =    m_Week[1];
		clCDateAndTime.bNongLi  =   m_bNongLi;
	
	}	
	
	
	public	void	getWeekTime( CDateAndTime clCDateAndTime, Byte week[] )
	{
		clCDateAndTime.iHour	=	(int) (m_lTime >> 16);
		clCDateAndTime.iMinute	=	(int) m_lTime & 0x0000ffff;
		
		if ( null != week )
		{
			int iLength	=	m_Week.length;
			for( int i = 0; i < iLength; ++i )
			{
				week[ i ]	=	m_Week[ i ];
			}		
		}

	}
	

	
	public long getOnceRemindMillis()
	{
		CDateAndTime  CDT = new  CDateAndTime();	
		getNormalTime( m_lTime ,CDT );
		long ltime = getTimeMillis(CDT.iHour, CDT.iMinute, CDT.iYear, CDT.iMonth, CDT.iDay );
		return  ltime;
	}
		
	
	private long getMonthRemindMillis()
	{
		CDateAndTime clCDateAndTime = new CDateAndTime( );
		
		getMonthTime(clCDateAndTime);
		
		Calendar clCalendar	=	Calendar.getInstance();
		int year = clCalendar.get(Calendar.YEAR);
		int month = clCalendar.get(Calendar.MONTH)+1;
		int curhour = clCalendar.get(Calendar.HOUR_OF_DAY);
		int curmin  = clCalendar.get(Calendar.MINUTE);
		int day = clCDateAndTime.iDay;
		int hour = clCDateAndTime.iHour;
		int minute = clCDateAndTime.iMinute;
		
		if(day < clCalendar.get(Calendar.DAY_OF_MONTH) )
		{	
			month+=1;
		}
		
		int remindsec = hour*3600 + minute*60;
		int cursec = curhour *3600 + curmin *60;
		if( day == clCalendar.get(Calendar.DAY_OF_MONTH) && remindsec <=  cursec )
		{
			month+=1;
		}

		getCorrectDay(day, month, year, clCDateAndTime);	

		long lTime  = getTimeMillis(hour, minute, clCDateAndTime.iYear, clCDateAndTime.iMonth-1, clCDateAndTime.iDay);
		return lTime;
		
	}
	
	private long getYearRemindMillis()
	{
		CDateAndTime clCDateAndTime = new CDateAndTime( );
		CDateAndTime tempDT = new CDateAndTime( );
		
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());

		long lCurTime	=	clCalendar.getTimeInMillis();
		
		getYearTime(clCDateAndTime);
		int  hour = clCDateAndTime.iHour;
		int  minute = clCDateAndTime.iMinute;
		int  day = clCDateAndTime.iDay;
		int  month = clCDateAndTime.iMonth;
		int  year  = clCalendar.get(Calendar.YEAR);
		
		if( m_bNongLi || m_Week[6]==1 )
		{
			
			ChineseCalendar.sCalendarLundarToSolar(year, month+1, day,  tempDT);
			year = tempDT.iYear;
			month= tempDT.iMonth-1;
			day = tempDT.iDay;	
		}

		clCalendar.set(Calendar.YEAR, year );
		clCalendar.set(Calendar.MONTH, month );
		clCalendar.set(Calendar.DAY_OF_MONTH, day );
		clCalendar.set(Calendar.HOUR_OF_DAY, hour );
		clCalendar.set(Calendar.MINUTE, minute );
		
		long  lnextTime = clCalendar.getTimeInMillis();
		
		if( lnextTime <= lCurTime )
		{
			year +=1;
		}
		
		if( m_bNongLi || m_Week[6]==1 )
		{
			
			ChineseCalendar.sCalendarLundarToSolar(year, clCDateAndTime.iMonth+1, clCDateAndTime.iDay,  tempDT);
			year = tempDT.iYear;
			month= tempDT.iMonth-1;
			day = tempDT.iDay;	
		}		
		
			
		long lTime  = getTimeMillis(hour, minute, year, month, day);
		
		return lTime;
	}
	
	
	public	String	getCountDownByStingForWidget( Notify notify)	//取得weidget式样使用的下次提醒时间的字符串
	{	
		String	strTemp	=	"";
		
		if( m_iType == CRemindType.Remind_Type_Once )
		{
			strTemp	=	SubCountdownByStringForWidget(  getOnceRemindMillis() );

		}else if( m_iType == CRemindType.Remind_Type_CountDown )
		{
			strTemp	=	SubCountdownByStringForWidget( notify.getNotifytime());

		}
		else if( m_iType == CRemindType.Remind_Type_Week ){

			long lFirstRemindTime	=	getFirstCycelRemindTime( null );
			strTemp	=	SubCountdownByStringForWidget( lFirstRemindTime );
		}
		else if( m_iType == CRemindType.Remind_Type_Year)
		{
			long lFirstRemindTime	=	getFirstCycelRemindTime( null );
			strTemp	=	SubCountdownByStringForWidget( lFirstRemindTime );
		}
		else if(m_iType == CRemindType.Remind_Type_Month)
		{
			long lFirstRemindTime	=	getFirstCycelRemindTime( null );
			strTemp	=	SubCountdownByStringForWidget( lFirstRemindTime );		
		}	
		else
		{
			//error
		}
		

		return	strTemp;

	}
	private	String	SubCountdownByString( long _lTime )
	{
		String	strTemp		=	null;		//返回null为失败
		
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());
		long	lCur		=	clCalendar.getTimeInMillis();
		
		long	lTemp	=	_lTime - lCur;
		if ( 0 > lTemp )
		{
			return	strTemp;	
		}	
		
		int	iTimeTemp		=	(int)(lTemp /1000 / 60 / 60 / 24 / 365);
		if ( 0 < iTimeTemp )
		{
			strTemp = "下次提醒 : " + String.valueOf(iTimeTemp) +"年  后";
			return	strTemp;
		}
		
		iTimeTemp	=	(int)(lTemp /1000 / 60 / 60 / 24 );
		if ( 0 < iTimeTemp )
		{
			int hour	=	(int)(( lTemp /1000 / 60 / 60 )	-	iTimeTemp * 24 );
			strTemp = "下次提醒 : " + String.valueOf(iTimeTemp) +"天 " +String.valueOf(hour) + "小时后";
			return	strTemp;
		}
		
		int	iHour	=	(int)(lTemp / 1000 / 60/ 60 );
		lTemp   -=	( iHour * 1000 * 60 * 60 );
		
		int	iMinute	=	(int)( lTemp / 1000 / 60 );	
		
		if( 0 < iHour || 0 < iMinute )
		{
			strTemp = "下次提醒 : " + String.valueOf(iHour) + "小时" + String.valueOf(iMinute) + "分钟  后";
			return	strTemp;
		}
		else
		{
			strTemp = "下次提醒 : 小于 1 分钟";
		}
		

		return	strTemp;
	}
	private	String	SubCountdownByStringForWidget( long _lTime )
	{

		String	strTemp		=	null;		//返回null为失败
	
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());
		long	lCur		=	clCalendar.getTimeInMillis();
		
		long	lTemp	=	_lTime - lCur;
		if ( 0 > lTemp )
		{
			return	strTemp;	
		}	
		
		int	iTimeTemp		=	(int)(lTemp /1000 / 60 / 60 / 24 / 365);
		if ( 0 < iTimeTemp )
		{
			strTemp = String.valueOf(iTimeTemp) +"年  后";
			return	strTemp;
		}

		CDateAndTime	clCurTime	=	new	CDateAndTime();
		getNormalTime( lCur,clCurTime);

		CDateAndTime	clRemindTime	=	new	CDateAndTime();
		getNormalTime( _lTime,clRemindTime);
	
		if(clCurTime.iYear == clRemindTime.iYear){
			if(( clCurTime.iMonth == clRemindTime.iMonth)
				&&( clCurTime.iDay== clRemindTime.iDay)){

				strTemp =strTemp.format("今天 %02d:%02d",clRemindTime.iHour,clRemindTime.iMinute) ;
				return	strTemp;

			}
		}

		clCalendar.set(clCurTime.iYear,clCurTime.iMonth,clCurTime.iDay);
		clCalendar.add(clCalendar.DATE,+1);	
		long tomorrow	=	clCalendar.getTimeInMillis();

		CDateAndTime	clTomorrow	=	new	CDateAndTime();
		getNormalTime( tomorrow,clTomorrow);
		
		if(clTomorrow.iYear == clRemindTime.iYear){
			if(( clTomorrow.iMonth == clRemindTime.iMonth)
					&&( clTomorrow.iDay== clRemindTime.iDay)){
					
				strTemp =strTemp.format("明天 %02d:%02d",clRemindTime.iHour,clRemindTime.iMinute) ;
				return	strTemp;
			}
		}

		strTemp = String.valueOf(clRemindTime.iMonth+1) +"月" +String.valueOf(clRemindTime.iDay)+"日" ;

		return	strTemp;
	}	

	
	
	public	long getTimeMillis( int iHour, int iMinute, int iYear, int iMonth, int iDay )
	{
		Calendar clCalendar	=	Calendar.getInstance();
		clCalendar.setTimeInMillis(System.currentTimeMillis());
		
		clCalendar.set(Calendar.SECOND, 0 );
		clCalendar.set(Calendar.MILLISECOND, 0 );
		
		clCalendar.set(Calendar.YEAR, iYear );
		clCalendar.set(Calendar.MONTH, iMonth );
		clCalendar.set(Calendar.DAY_OF_MONTH, iDay );
		clCalendar.set(Calendar.HOUR_OF_DAY, iHour );
		clCalendar.set(Calendar.MINUTE, iMinute );
		
		long ltime = clCalendar.getTimeInMillis();
		
		return ltime;
	}
	
		

	public	String	getCountDownBySting()	//取得距离下次提醒还有多长时间的字符串
	{	
		String	strTemp	=	null;
		
		if ( m_iType == CRemindType.Remind_Type_Once )
		{	
			strTemp	=	SubCountdownByString( getOnceRemindMillis() );
		}
		else if( m_iType == CRemindType.Remind_Type_CountDown  )
		{
			strTemp	=	SubCountdownByString( m_lTime );
		}	
		else if ( m_iType == CRemindType.Remind_Type_Week )
		{

		
			WeekCountDown	clWeekCountDown	=	new	WeekCountDown();
			
			getFirstCycelRemindTime( clWeekCountDown );

			if( clWeekCountDown.iType == CRemindType.WeekNext )
			{
				if( clWeekCountDown.iWeek == 0 )
				{
					strTemp	=	"下次提醒 : 本周 日 ";
				}		
				else if( clWeekCountDown.iWeek >= 0 && clWeekCountDown.iWeek <= 6 )
				{
					strTemp	=	"下次提醒 : 下周 " + getDayofWeek(clWeekCountDown.iWeek );
				}
				
			}
			else if ( clWeekCountDown.iType == CRemindType.WeekCurrent )
			{
				if( clWeekCountDown.iWeek >= 0 && clWeekCountDown.iWeek <= 6 )
				{
					Calendar clCalendar     =     Calendar. getInstance();
					clCalendar.setTimeInMillis( System.currentTimeMillis() );
					int	iCurr	=	clCalendar.get(Calendar.DAY_OF_WEEK);
					-- iCurr;
					if ( iCurr == clWeekCountDown.iWeek )
					{
						strTemp	=	"下次提醒 : 今天 ";
					}
					else
					{
						strTemp	=	"下次提醒 : 本周 " + getDayofWeek(clWeekCountDown.iWeek );
					}
				}
			}
			else
			{
				//error
			}
		}
		else if ( m_iType == CRemindType.Remind_Type_Month )
		{
			long  lTime  = getMonthRemindMillis();
			strTemp	=	SubCountdownByString( lTime );
		}
		else if ( m_iType == CRemindType.Remind_Type_Year)
		{
			long  lTime  = getYearRemindMillis();
			strTemp	=	SubCountdownByString( lTime );	
		}
		
		else
		{
			//error
		}
		

		return	strTemp;

	}	
	
	//根据当前系统时间，计算取得循环提醒 的第一次要提醒的时间
	//当提醒到来时再次调用该方法取得下一个提醒的时间
	public	long	getFirstCycelRemindTime( WeekCountDown _clWeekCountDown )
	{
		long	lTimeInnerMax	=	-1;				
		long	lTimeInnerMin	=	-1;
		long	lTimeInner		=	-1;			//返回值
		if ( m_iType == CRemindType.Remind_Type_Week )	
		{
			//取得当前时间
			Calendar clCalendar	=	Calendar.getInstance();
			clCalendar.setTimeInMillis(System.currentTimeMillis());

			long	lCurTimer	=	clCalendar.getTimeInMillis();
//			 Log.d("CurrentTime:",  String.format("%04d/%02d/%02d %02d:%02d DayofWeek:%2d", clCalendar.get(Calendar.YEAR), clCalendar.get(Calendar.MONTH)+1
//						, clCalendar.get(Calendar.DAY_OF_MONTH), clCalendar.get(Calendar.HOUR_OF_DAY), clCalendar.get(Calendar.MINUTE), clCalendar.get(Calendar.DAY_OF_WEEK)));	

			long	lTempTime	=	-1;					//临时变量
			long	lMinTime	=	Long.MAX_VALUE;		//差值为正的最小时间为下次提醒时间，初始化为整数最大值
			long	lMaxTime	=	-1;					//差值为负的最小时间为下周的第一个提醒时间，初始化为负数最大值
			
			int		iType		=	CommonDefine.g_int_Invalid_ID;
			int		iWeek	=	CRemindType.WeekInvalid;
			int		iWeekNext	=	CRemindType.WeekInvalid;
			int		iWeekCurrent	=	CRemindType.WeekInvalid;
			
			int	bWeekTemp[]	=	new	int[ 7 ];
			for( int i = 0; i < 6; ++i )
			{
				bWeekTemp[ i + 1 ]	=	m_Week[i];
			}
			
			bWeekTemp[ 0 ]	=	m_Week[6];
			
			for	( int i = 0; i < 7; ++i )
			{
				 if ( 1 == bWeekTemp[i] )
				 {
					 clCalendar.setTimeInMillis(System.currentTimeMillis());
					 clCalendar.set(Calendar.SECOND, 0);
					 clCalendar.set(Calendar.MILLISECOND, 0);
					 clCalendar.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY + i );
					 int	hour	=	(int)m_lTime >> 16;
					 int	minute	=	(int)m_lTime & 0x0000ffff;
					 clCalendar.set( Calendar.HOUR_OF_DAY, hour);
					 clCalendar.set( Calendar.MINUTE, minute);
					 lTempTime	=	clCalendar.getTimeInMillis();
					 
					 clCalendar.setTimeInMillis(lTempTime);
//					 Log.d("LastWeek:",String.format("%04d/%02d/%02d %02d:%02d DayofWeek:%2d", clCalendar.get(Calendar.YEAR), clCalendar.get(Calendar.MONTH)+1
//								, clCalendar.get(Calendar.DAY_OF_MONTH), clCalendar.get(Calendar.HOUR_OF_DAY), clCalendar.get(Calendar.MINUTE), clCalendar.get(Calendar.DAY_OF_WEEK)));
					 
					 long	lMinTemp	=	lTempTime	-	lCurTimer;
					 
					 if ( lMinTemp > 0 )				//在本星期提醒
					 {					 
						 if ( lMinTemp < lMinTime )
						 {
							 lMinTime			=	lMinTemp;	//保存最小的值
							 lTimeInnerMin		=	lTempTime;
							 iWeekCurrent		=	i;
						 }						 
					 }
					 else								//在下星期提醒
					 {
						 if ( lMinTemp < lMaxTime )
						 {
							 lMaxTime			=	lMinTemp;
							 lTimeInnerMax		=	lTempTime;
							 clCalendar.setTimeInMillis(lTimeInnerMax);
							 iWeekNext			=	i;
//							 Log.d("RemindInfo:", String.format("%04d/%02d/%02d %02d:%02d DayofWeek:%2d", clCalendar.get(Calendar.YEAR), clCalendar.get(Calendar.MONTH)+1
//										, clCalendar.get(Calendar.DAY_OF_MONTH), clCalendar.get(Calendar.HOUR_OF_DAY), clCalendar.get(Calendar.MINUTE), clCalendar.get(Calendar.DAY_OF_WEEK)));
						 }
					 }
				 }
			}
			
			if ( lMinTime == Long.MAX_VALUE )				//说明提醒在下个星期
			{
				lTimeInnerMax	+=	ONE_WEEK_TIME;
				
				iType	=	CRemindType.WeekNext;
				iWeek	=	iWeekNext;
				clCalendar.setTimeInMillis( lTimeInnerMax );
//				 Log.d("ResultMax", String.format("%04d/%02d/%02d %02d:%02d DayofWeek:%2d", clCalendar.get(Calendar.YEAR), clCalendar.get(Calendar.MONTH)+1
//							, clCalendar.get(Calendar.DAY_OF_MONTH), clCalendar.get(Calendar.HOUR_OF_DAY), clCalendar.get(Calendar.MINUTE), clCalendar.get(Calendar.DAY_OF_WEEK)));
				lTimeInner	=	lTimeInnerMax;
			}
			else
			{
				clCalendar.setTimeInMillis( lTimeInnerMin );
				iType	=	CRemindType.WeekCurrent;
				iWeek	=	iWeekCurrent;
//				 Log.d("ResultMin",String.format("%04d/%02d/%02d %02d:%02d DayofWeek:%2d", clCalendar.get(Calendar.YEAR), clCalendar.get(Calendar.MONTH)+1
//							, clCalendar.get(Calendar.DAY_OF_MONTH), clCalendar.get(Calendar.HOUR_OF_DAY), clCalendar.get(Calendar.MINUTE), clCalendar.get(Calendar.DAY_OF_WEEK)));
				lTimeInner	=	lTimeInnerMin;
			}
			
			if( null != _clWeekCountDown )
			{
				_clWeekCountDown.iType	=	iType;
				_clWeekCountDown.iWeek	=	iWeek;
			}
			
			return	lTimeInner;
		}
		else if( m_iType == CRemindType.Remind_Type_Month )
		{
			long lTime  = getMonthRemindMillis();	
			return  lTime;
		}
		else if ( m_iType == CRemindType.Remind_Type_Year )
		{
			long lTime  = getYearRemindMillis();	
			return  lTime;
		}
		
			
		return	CommonDefine.E_FAIL;
	}
	

	CRemindInfo( int iType )
	{
		int	length		=	m_Week.length;
		for ( int i = 0; i < length; ++i )
		{
			m_Week[ i ]	=	(byte)CommonDefine.g_int_Invalid_ID;
		}
		
		m_iType			=	iType;
		m_lTime			=	0;
		
		m_iRemindAble	=	CommonDefine.g_int_Invalid_ID;
		m_iIsRemind		=	CommonDefine.g_int_Invalid_ID;
	}
	
	boolean		IsEql( CRemindInfo _clCRemindInfo )
	{
		if( _clCRemindInfo.m_iRemindAble != m_iRemindAble || _clCRemindInfo.m_iIsRemind != m_iIsRemind )
		{
			return	false;
		}
		
		if( _clCRemindInfo.m_iType != m_iType )
		{
			return	false;
		}
		
		
		
		if( _clCRemindInfo.m_iType == CRemindType.Remind_Type_Week )
		{
			int	length		=	m_Week.length;
			for ( int i = 0; i < length; ++i )
			{
				if( (int) m_Week[ i ] != (int) _clCRemindInfo.m_Week[ i ] )
				{
					return	false;
				}
			}
		}
		else if( _clCRemindInfo.m_iType == CRemindType.Remind_Type_Once )
		{
			if( _clCRemindInfo.m_lTime != m_lTime )
			{
				return	false;
			}
		}
		else if( _clCRemindInfo.m_iType == CRemindType.Remind_Type_Year )
		{
			if( _clCRemindInfo.m_lTime != m_lTime ||  _clCRemindInfo.m_bNongLi != m_bNongLi  )
			{
				return	false;
			}
			
			if( (int)_clCRemindInfo.m_Week[1] !=(int) m_Week[1] )
			{
				return false;
			}
			
			if((int)_clCRemindInfo.m_Week[0] != (int)m_Week[0]   )
			{
				return false;
			}

			
		}
		else if( _clCRemindInfo.m_iType == CRemindType.Remind_Type_Month )
		{
			if( _clCRemindInfo.m_lTime != m_lTime ||  (int)_clCRemindInfo.m_MonthDay != (int)m_MonthDay )
			{
				return	false;
			}
			
		}
			
		
		return	true;
	}
	
	
	
	
	public void  getCorrectDay(int iday, int imonth , int iYear,  CDateAndTime  d )
	{
		int maxday[]={ 0, 31, 28, 31, 30, 31,30, 31, 31,  30, 31, 30, 31 };
		
		if(imonth == 13 )
		{
			imonth = 1;
			iYear +=1;
		}
	
		if(iYear%4==0&&iYear%100!=0||iYear%400==0)
		{
			maxday[2] = 29;
		}
		
		if(iday > maxday[imonth])
		{
			iday = maxday[imonth];
			
		}
		d.iDay = iday;
		d.iMonth = imonth;
		d.iYear = iYear;
					
	}
	
	
	
    static	String	getDayofWeek( int iWeek )
    {
    	String	strTemp	=	null;
    	switch( iWeek )
    	{
    	case	0:
    		strTemp	=	"日";
    		break;
    	case	1:
    		strTemp	=	"一";
    		break;
    	case	2:
    		strTemp	=	"二";
    		break;
    	case	3:
    		strTemp	=	"三";
    		break;
    	case	4:
    		strTemp	=	"四";
    		break;
    	case	5:
    		strTemp	=	"五";
    		break;
    	case	6:
    		strTemp	=	"六";
    		break;
    	}
    	
    	return	strTemp;
    }
    
    
	public boolean	checkTime()
	{
		boolean		bflg	=	false;
		
		if(  m_iType == CRemindType.Remind_Type_Once  )
		{
			Calendar clCalendar	=	Calendar.getInstance();
			clCalendar.setTimeInMillis(System.currentTimeMillis());
			
			long	lCur	=	clCalendar.getTimeInMillis();
			if ( lCur > getOnceRemindMillis() )
			{
				bflg	=	false;
			}
			else
			{
				bflg	=	true;
			}
		}
		else if( m_iType == CRemindType.Remind_Type_CountDown )
		{
			if( m_lTime <= 0 )
			{
				bflg	=	false;
			}
			else
			{
				bflg	=	true;
			}
		}
		else if( m_iType == CRemindType.Remind_Type_Week )
		{
			
			for ( int i = 0; i < 7; ++i )
			{
				if ( 1 == m_Week[ i ] )
				{
					bflg	=	true;
					break;
				}
			}
		}
		else if( m_iType == CRemindType.Remind_Type_Month )
		{
			bflg	=	true;
		}
		else if( m_iType == CRemindType.Remind_Type_Year )
		{
			bflg	=	true;
		}
		
		return	bflg;
	}  
	
}

class	WeekCountDown
{
	int	iType;	//本周还是下周提醒
	int	iWeek;	//周几提醒
	
	WeekCountDown()
	{
		iType	=	CommonDefine.g_int_Invalid_ID;
		iWeek	=	CommonDefine.g_int_Invalid_ID;
	}
}

